package com.cuanbo.dualscreenteachzhu.utils;

import android.text.TextUtils;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

/**
 * ip工具类
 */
public class IpUtil {

    /**
     * 获取本机ip地址
     */
    public static String getIpAddressString() {
        //return "192.168.1.9";
        try {
            for (Enumeration<NetworkInterface> enNetI = NetworkInterface
                    .getNetworkInterfaces(); enNetI.hasMoreElements(); ) {
                NetworkInterface netI = enNetI.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = netI
                        .getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (inetAddress instanceof Inet4Address && !inetAddress.isLoopbackAddress()) {
                        return inetAddress.getHostAddress();
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return "0.0.0.0";
    }

    /**
     *获取本机ip地址
     */
    public static InetAddress getIpAddress() {
        try {
            for (Enumeration<NetworkInterface> enNetI = NetworkInterface
                    .getNetworkInterfaces(); enNetI.hasMoreElements(); ) {
                NetworkInterface netI = enNetI.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = netI
                        .getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (inetAddress instanceof Inet4Address && !inetAddress.isLoopbackAddress()) {
                        return inetAddress;
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 检查ip是否可达
     */
    public static boolean sendPingRequest(String ipAddress) {
        try {
            if (TextUtils.isEmpty(ipAddress)) return false;
            LogUtil.add("IP地址", ipAddress, "IP网络检查", false);
            InetAddress geek = InetAddress.getByName(ipAddress);
            if (geek.isReachable(5000)) {
                LogUtil.add("结果", "ip可以正常访问", "IP网络检查", false);
                return true;
            } else {
                LogUtil.add("结果", "ip不可达", "IP网络检查", false);
                return false;
            }
        } catch (Exception e) {
            LogUtil.addError("-----------------IP网络检查异常------------------");
            LogUtil.addError("IP地址=" + ipAddress);
            LogUtil.addError(e);
            LogUtil.addError("------------------------------------------------");
        }
        return false;
    }

    /**
     * 检查中控IP是否可达
     */
/*    public static void checkMatrixIPPing() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String strIp = null;
                try {
                    strIp = App.configInfo.getMatrixIP();
                    if (TextUtils.isEmpty(strIp)) return;
                    App.matrixIPNetState = sendPingRequest(strIp);
                    LogUtil.add("中控", "IP=" + strIp + " 网络=" + App.matrixIPNetState, "IP网络检查", false);
                } catch (Exception e) {
                    LogUtil.addError("-----------------中控IP网络检查异常------------------");
                    LogUtil.addError("中控地址=" + strIp);
                    LogUtil.addError(e);
                    LogUtil.addError("------------------------------------------------");
                }
            }
        }).start();
    }*/

}
